package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.IntegralRuleBo;
import com.rla.domain.vo.IntegralRuleVo;
import com.rla.domain.IntegralRule;
import com.rla.mapper.IntegralRuleMapper;
import com.rla.service.IIntegralRuleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分规则Service业务层处理
 *
 * @author rla
 * @date 2025-09-27
 */
@RequiredArgsConstructor
@Service
public class IntegralRuleServiceImpl implements IIntegralRuleService {

    private final IntegralRuleMapper baseMapper;

    /**
     * 查询积分规则
     */
    @Override
    public IntegralRuleVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询积分规则列表
     */
    @Override
    public TableDataInfo<IntegralRuleVo> queryPageList(IntegralRuleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IntegralRule> lqw = buildQueryWrapper(bo);
        Page<IntegralRuleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分规则列表
     */
    @Override
    public List<IntegralRuleVo> queryList(IntegralRuleBo bo) {
        LambdaQueryWrapper<IntegralRule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IntegralRule> buildQueryWrapper(IntegralRuleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IntegralRule> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIntegralGet()), IntegralRule::getIntegralGet, bo.getIntegralGet());
        lqw.like(StringUtils.isNotBlank(bo.getIntegralName()), IntegralRule::getIntegralName, bo.getIntegralName());
        lqw.eq(bo.getConvertRatio() != null, IntegralRule::getConvertRatio, bo.getConvertRatio());
        lqw.eq(StringUtils.isNotBlank(bo.getCostAllocation()), IntegralRule::getCostAllocation, bo.getCostAllocation());
        lqw.eq(bo.getConsumeAmount() != null, IntegralRule::getConsumeAmount, bo.getConsumeAmount());
        lqw.eq(bo.getGetIntegral() != null, IntegralRule::getGetIntegral, bo.getGetIntegral());
        lqw.eq(StringUtils.isNotBlank(bo.getRestrict()), IntegralRule::getIsRestrict, bo.getRestrict());
        lqw.eq(bo.getRestrictDay() != null, IntegralRule::getRestrictDay, bo.getRestrictDay());
        lqw.eq(bo.getRestrictIntegral() != null, IntegralRule::getRestrictIntegral, bo.getRestrictIntegral());
        lqw.eq(bo.getBasisUpdateTime() != null, IntegralRule::getBasisUpdateTime, bo.getBasisUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getBasisUpdateBy()), IntegralRule::getBasisUpdateBy, bo.getBasisUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getShop()), IntegralRule::getShop, bo.getShop());
        lqw.eq(StringUtils.isNotBlank(bo.getConsumeRestrict()), IntegralRule::getConsumeRestrict, bo.getConsumeRestrict());
        lqw.eq(bo.getConsumeRestrictDay() != null, IntegralRule::getConsumeRestrictDay, bo.getConsumeRestrictDay());
        lqw.eq(bo.getConsumeRestrictIntegral() != null, IntegralRule::getConsumeRestrictIntegral, bo.getConsumeRestrictIntegral());
        lqw.eq(bo.getConsumeUpdateTime() != null, IntegralRule::getConsumeUpdateTime, bo.getConsumeUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getConsumeUpdateBy()), IntegralRule::getConsumeUpdateBy, bo.getConsumeUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getIndate()), IntegralRule::getIndate, bo.getIndate());
        lqw.eq(bo.getIndateDate() != null, IntegralRule::getIndateDate, bo.getIndateDate());
        lqw.eq(bo.getExecTime() != null, IntegralRule::getExecTime, bo.getExecTime());
        lqw.eq(bo.getClearUpdateTime() != null, IntegralRule::getClearUpdateTime, bo.getClearUpdateTime());
        lqw.eq(bo.getClearUpdateBy() != null, IntegralRule::getClearUpdateBy, bo.getClearUpdateBy());
        return lqw;
    }

    /**
     * 新增积分规则
     */
    @Override
    public Boolean insertByBo(IntegralRuleBo bo) {
        IntegralRule add = BeanUtil.toBean(bo, IntegralRule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改积分规则
     */
    @Override
    public Boolean updateByBo(IntegralRuleBo bo) {
        IntegralRule update = BeanUtil.toBean(bo, IntegralRule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IntegralRule entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分规则
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
